package com.naiterui.ehp.bs.cms.modules.user.controller;

import java.io.IOException;
import java.util.List;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.naiterui.ehp.bp.bo.esign.EsignNoSecretBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.user.feign.PharmacisFeignClient;
import com.naiterui.ehp.bs.cms.modules.user.service.IPharmacistService;
import com.naiterui.ehp.bs.cms.modules.user.vo.NationVO;
import com.naiterui.ehp.bs.cms.modules.user.vo.PharmacistDetailVO;
import com.naiterui.ehp.bs.cms.modules.user.vo.PharmacistVO;
import com.naiterui.ehp.bs.cms.modules.user.vo.record.RecordInfoVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Api(tags = {SwaggerTag.CMS + SwaggerTag.USER})
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/user/pharmacis")
public class PharmacistController {

    private final IPharmacistService pharmacistService;
    private final PharmacisFeignClient pharmacisFeignClient;

    /**
     * 药师列表
     *
     * @return
     */
    @SysLog("药师列表")
    @GetMapping("/list")
    @ApiOperation("药师列表")
    @PreAuthorize("@p.check('user:pharmacis:list')")
    public PageVO<PharmacistVO> list(PageParamsVO pageParamsVO, PharmacistVO pharmacistVO) {
        return this.pharmacistService.queryPage(pageParamsVO, pharmacistVO);
    }

    @SysLog("药师详情")
    @GetMapping("/{id}")
    @ApiOperation("药师详情")
    @ApiImplicitParam(name = "id", value = "药师ID", required = true, dataType = "long", paramType = "query")
    @PreAuthorize("@p.check('user:pharmacis:list')")
    public PharmacistDetailVO detail(@PathVariable("id") Long id) {
        return this.pharmacistService.queryById(id);
    }

    @SysLog("添加药师")
    @PostMapping("/add")
    @ApiOperation("添加药师")
    @PreAuthorize("@p.check('user:pharmacist:add')")
    public Long add(@RequestBody String json) throws BusinessException {
        log.info("添加药师，参数信息 {}", json);
        return this.pharmacisFeignClient.add(json);
    }

    @SysLog("修改药师")
    @PostMapping("/update")
    @ApiOperation("修改药师")
    @PreAuthorize("@p.check('user:pharmacist:edit')")
    public Long update(@RequestBody String json) throws BusinessException {
        return this.pharmacisFeignClient.update(json);
    }

   /*
    @ApiOperation("药师文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "资源文件", dataType = "file", paramType = "body", required = true),
            @ApiImplicitParam(name = "type", value = "文件类型 1:资格证 2:身份证照片", dataType = "String", paramType = "path", required =
                    true)
    })
    @PostMapping("/upload/{type}")
    @PreAuthorize("@p.check('user:pharmacis:update')")
    public ResponseEntity<String> upload(@NotNull(message = "上传文件不能为null") MultipartFile file, @PathVariable(
            "type") Integer type) throws IOException {
        String url = this.pharmacistService.upload(file, type);
        return ResponseEntity.ok(url);
    }*/

   @ApiOperation("民族")
    @GetMapping("/nations")
    @PreAuthorize("@p.check('user:pharmacis:list')")
    public List<NationVO> nations() {
        return this.pharmacistService.getNations();
    }

    @ApiOperation("获取完善药师备案信息")
    @ApiImplicitParam(name = "pharmacistId", value = "药师ID", required = true, dataType = "long", paramType = "query")
    @SysLog("获取完善药师备案信息")
    @GetMapping("/record")
    @PreAuthorize("@p.check('user:pharmacis:record')")
    public RecordInfoVO recordInfo(Long pharmacistId) {
        return this.pharmacistService.recordInfo(pharmacistId);
    }

    @ApiOperation("完善药师备案信息")
    @SysLog("完善药师备案信息")
    @PostMapping("/record")
    @PreAuthorize("@p.check('user:pharmacis:record')")
    public void updateRecordInfo(@RequestBody RecordInfoVO recordInfoVO) throws BusinessException {
        this.pharmacistService.updateRecordInfo(recordInfoVO);
    }

    @ApiOperation("药师相关文件上传")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "文件", dataType = "__file", paramType = "form", required = true),
        @ApiImplicitParam(name = "type", value = "文件类型", dataType = "int", paramType = "path", required = true)})
    @PostMapping("/upload/{type}")
    @PreAuthorize("@p.check('user:pharmacis:record')")
    public ResponseVO<String> upload(@NotNull(message = "上传文件不能为null") MultipartFile file, @PathVariable int type) throws IOException {
        log.info("药师相关文件上传 type {}, file {}", type, file);
        String url = this.pharmacistService.uploadFile(type, file);
        return PublicService.returnResponseVO(url);
    }


    @SysLog("修改药师账号状态")
    @ApiOperation("修改药师账号状态 0:正常，1:禁用")
    @GetMapping("/{pharmacistId}/{status}")
    @PreAuthorize("@p.check('user:pharmacis:status')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pharmacistId", value = "药师ID", dataType = "long", paramType = "path", required = true),
            @ApiImplicitParam(name = "status", value = "状态 0:正常，1:禁用", dataType = "String", paramType = "path", required = true)
    })
    public void accountStatus(@PathVariable("pharmacistId") Long pharmacistId,
                              @PathVariable("status")
                              @Max(value = 1, message = "状态不正确")
                              @Min(value = 0, message = "状态不正确") Integer status) throws BusinessException {
        log.info("CMS修改药师账号状态：pharmacistId={}，status={}", pharmacistId, status);
        pharmacisFeignClient.accountStatus(pharmacistId, status);
    }

    @SysLog("获取药师免密状态")
    @GetMapping("/getNoSecret")
    @ApiOperation("获取药师免密状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pharmacistId", value = "药师id", required = true, dataType = "long")
    })
    public ResponseVO<EsignNoSecretBO> getNoSecret(Long pharmacistId) throws BusinessException {
        log.info("获取免密状态：pharmacistId={}", pharmacistId);
        EsignNoSecretBO noSecretBO = pharmacisFeignClient.getNoSecretByPharmacistId(pharmacistId);
        return PublicService.returnResponseVO(noSecretBO);
    }
}
